<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="车牌号码" prop="carNumber">
        <el-input
          v-model="queryParams.carNumber"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="jobStatus">
        <el-select
          v-model="queryParams.jobStatus"
          placeholder="请选择车辆状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.car_job_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="到期状态" prop="expireStatus">
        <el-select
          v-model="queryParams.expireStatus"
          placeholder="请选择到期状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.car_job_expirestatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- 搜索和重置 -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 新增、导出、删除 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tms:car:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tms:car:remove']"
          >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tms:car:export']"
          >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-timer"
          size="mini"
          @click="syncMsg"
          v-hasPermi="['tms:car:syncPullCars']"
          >同步gps车辆信息
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      :border="true"
      v-loading="loading"
      :data="carList"
      style="width: 100%"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column width="50" label="序号" align="center" prop="id" />
      <el-table-column label="车牌号码" align="center" prop="carNumber" />
      <el-table-column label="体积(方)" align="center" prop="carVolume" />
      <el-table-column label="载重(吨)" align="center" prop="carLoad" />
      <el-table-column label="长度(米)" align="center" prop="carLength" />
      <el-table-column label="宽度(米)" align="center" prop="carWidth" />
      <el-table-column label="高度(米)" align="center" prop="carHeight" />
      <el-table-column label="状态" align="center" prop="jobStatus" width="85">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.car_job_status"
            :value="scope.row.jobStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="到期状态"
        align="center"
        prop="expireStatus"
        width="90"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.car_job_expirestatus"
            :value="scope.row.expireStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="gps定位Id" align="center" prop="gpsId" />
      <el-table-column
        label="创建人"
        width="80"
        align="center"
        prop="createBy"
      />
      <el-table-column
        width="100"
        label="创建时间"
        align="center"
        prop="updateTime"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-d-arrow-right"
            @click="handleView(scope.row)"
            v-hasPermi="['tms:car:edit']"
            >查看更多
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-location-information"
            :style="{ display: scope.row.gpsId == '' ? 'none' : '' }"
            @click="handleGps(scope.row)"
            v-hasPermi="['tms:car:location']"
            >定位
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tms:car:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <AddCarMsg :title="title" :open="open" v-on:close="refresh"/>
    <EditCarMsg
      :title="title"
      :open="openView"
      :form="form"
      v-on:close="refresh"
    />
    <GpsCar :title="title" :open="showGps" :gpsInfo ="gpsInfo" v-on:close="refresh"/>
  </div>
</template>

<script>
import { delCar, getCar, listCar, gpsCar,syncCar } from "@/api/tms/car";
import AddCarMsg from "./components/AddCarMsg.vue";
import EditCarMsg from "./components/EditCarMsg.vue";
import GpsCar from './components/GpsCar.vue'
export default {
  dicts: ["car_job_status", "car_job_expirestatus"],
  name: "Car",
  components: {
    AddCarMsg,
    EditCarMsg,
    GpsCar
  },
  data() {
    return {
      fullscreenFlag: false,
      Gps: false,
      gpsName: "",
      gpsInfo: {},
      showGps: false,
      loading: false,
      // 显示缓冲状态
      huanchong: false,
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      openView: false,
      // 是否显示行驶证信息弹出层
      driveView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNumber: null,
        carVolume: null,
        carLoad: null,
        carLength: null,
        carWidth: null,
        carHeight: null,
        loadMass: null,
        carImgUrl: null,
        licenseNumber: null,
        licenseImgUrl: null,
        licenseValidTime: null,
        carCheckValid: null,
        enrollTime: null,
        scrapTime: null,
        jobStatus: null,
        gpsId: null,
        deleted: null,
      },
      // 表单参数
      form: {},
      gpsId: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 同步gps信息
    syncMsg(){
      syncCar().then(res=>{
        if (res.code!=200) {
          this.$message.warning('信息同步中，请稍后！')
        }else{
          this.$message.success('已同步!')
        }
      })
    },
    // 获取车辆gps信息
    handleGps(row) {
      // console.log(row);
      gpsCar(row.gpsId).then((res) => {
        this.gpsInfo = res.data;
        // console.log(this.gpsInfo);
      });
      this.showGps = true;
      this.title = "查看车辆GPS";
    },
    /** 刷新 */
    refresh() {
      this.showGps = false;
      this.open = false;
      this.openView = false;
      this.getList();
    },
   
    // 表单隔行变色
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
    /** 查看车辆信息 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getCar(id).then((response) => {
        this.form = response.data;
        this.openView = true;
        this.title = "车辆基本信息";
      });
    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then((response) => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carNumber: null,
        carVolume: null,
        carLoad: null,
        carLength: null,
        carWidth: null,
        carHeight: null,
        loadMass: null,
        carImgUrl: null,
        licenseNumber: null,
        licenseImgUrl: null,
        licenseValidTime: null,
        carCheckValid: null,
        enrollTime: null,
        scrapTime: null,
        jobStatus: null,
        gpsId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加车辆信息";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除车辆信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCar(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tms/car/export",
        {
          ...this.queryParams,
        },
        `car_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-table .warning-row {
  background: #f7faff;
}

.el-table .success-row {
  background: #ebf1fb;
}
.message {
  width: 100%;
  /* text-align: center; */
  display: flex;
  /* justify-content: center; */
  margin-top: -30px;
  margin-bottom: 0px;
  /* background-color: pink; */
}
.message table {
  width: 100%;
}
.message table tr td:first-child {
  text-align: right;
}
.message table tr td {
  padding: 2px 10px;
}

.fullScreenIcon {
  position: absolute;
  top: 20px;
  right: 40px;
  padding: 0;
  background: transparent;
  border: none;
  outline: none;
  cursor: pointer;
  font-size: 16px;
}

.fullScreenIcon:hover {
  color: #409eff;
}
</style>
